package jsyf.oa.web.device;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.device.DevicelogisticsFacade;
import jsyf.oa.web.socket.MyWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.TextMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by y on 2018/6/28.
 */
@Api(value = "设备物流信息更改接口", description = "设备物流信息更改接口")
@Controller
@RequestMapping("/devicelogistics")
public class DevicelogisticsController {
    @Resource
    private DevicelogisticsFacade devicelogisticsFacade;
    @Autowired
    private MyWebSocketHandler myWebSocketHandler;
    @ResponseBody
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "添加物流信息",
            notes = "添加物流信息 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonResult = new JSONObject();
        jsonResult.put("id",this.devicelogisticsFacade.add(jsonData));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonResult));
    }

    @ResponseBody
    @RequestMapping(value = "/getDeviceTypeListBySampleId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询物流更改记录",
            notes = "查询物流更改记录 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'sampleId':''}", dataType = "String", paramType = "body")
    public void getDeviceTypeListBySampleId(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = this.devicelogisticsFacade.getDeviceTypeListBySampleId(jsonData);
        if (null != records) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("查找失败"));
        }
    }

    @ResponseBody
    @RequestMapping(value = "/outBound.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "新机出库",
            notes = "新机出库 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    public void outBound(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonResult = this.devicelogisticsFacade.outBound(jsonData);
        //发送消息
        sendMessage(jsonResult.getString("message"));
        jsonResult.remove("message");
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonResult));
    }
    @ResponseBody
    @RequestMapping(value = "/updateOutRecord.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "修改出库记录",
            notes = "修改出库记录 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    public void updateOutRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject results = devicelogisticsFacade.updateOutRecord(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(results));
    }
    @ResponseBody
    @RequestMapping(value = "/updateMoveRecord.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "修改移库记录",
            notes = "修改移库记录 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    public void updateMoveRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject results = devicelogisticsFacade.updateMoveRecord(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(results));
    }
    @ResponseBody
    @RequestMapping(value = "/updateExitRecord.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "修改退货记录",
            notes = "修改退货记录 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    public void updateExitRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject results = devicelogisticsFacade.updateExitRecord(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(results));
    }
    private void sendMessage(String jsonData) {
        List<JSONObject> jsonObjectList = null;
        if (!org.apache.commons.lang3.StringUtils.isBlank(jsonData)) {
            jsonObjectList = FastJsonConvert.convertJSONToArray(jsonData, JSONObject.class);
        }
        if (null != jsonObjectList && jsonObjectList.size() > 0) {
            TextMessage textMessage = new TextMessage(BaseUtils.succsessArrHourTimeRespond(jsonObjectList), true);
            try {
                myWebSocketHandler.sendMsgToAllUsers(textMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
